//
//  DBBaseController.swift
//  大波微博swift
//
//  Created by 大波 on 16/2/27.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit

class DBBaseController: UITableViewController {

    //这个属性用来控制用户有没有登陆,如果登陆了就不走访客模式加载vistorView
    var isUserLogin = DBUserAccountViewModel.shareUserAccountViewModel.isUserLogin
    
    override func loadView() {
        
        //如果用户登陆了就加载tableView   
        //反之则跳到设置访客视图的方法
        isUserLogin ? super.loadView() : setVistorView()
    }
    
    ///更新访客界面
    func setVistorView() {
        
        view = vistorView
        
        //print(__FUNCTION__)
        if self is DBHomeController {
            
            vistorView.setIconViewRotate()
            
        } else if self is DBDiscoverController {
            
            vistorView.setupVistorView("visitordiscover_image_message", titleString: "录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
            
        } else if self is DBMessageController {
            
            vistorView.setupVistorView("visitordiscover_image_message", titleString: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
            
        } else if self is DBProfileController {
            
            vistorView.setupVistorView("visitordiscover_image_profile", titleString: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")   
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        vistorView.vistorViewDelegate = self
        
        //注册和登陆按钮点击事件 闭包
//        loginBtnAndRegisterBtnDidClickClosure()
        
        //导航栏按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "registerBtnDidClick")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: UIBarButtonItemStyle.Done, target: self, action: "loginBtnDidClick")
    }
    
    //MARK: - 注册和登陆按钮点击事件
    ///注册和登陆按钮点击闭包回调
    func loginBtnAndRegisterBtnDidClickClosure() {
        
        vistorView.loginBtnDidClickClosure = {
            
            print("登录按钮闭包执行了")
        }
        
        vistorView.registerDidClickClosure = {
            
            print("注册按钮闭包执行了")
        }
    }
    
    //MARK: - 懒加载
    lazy var vistorView: DBVistorView = DBVistorView()
}

//MARK: - 访客界面按钮点击代理方法
extension DBBaseController: DBVistorViewDelegate {
    
    func loginBtnDidClick() {
        
//        print("登陆按钮的代理方法实现了")
        
        presentViewController(UINavigationController(rootViewController: DBOauthController()), animated: true, completion: nil)
    }
    
    func registerBtnDidClick() {
        
//        print("注册按钮的代理方法实现了")
    }
}